﻿using System;
using System.Linq.Expressions;
using DapperLambdaSQL.Factory;
using DapperLambdaSQL.Model;
using DapperLambdaSQL.Utils;
using Microsoft.Extensions.Logging;

namespace DapperLambdaSQL
{
    public abstract class DbContext
    {
        /// <summary>
        /// 数据库连接提供者
        /// </summary>
        protected IDbConnectionFactory connectionFactory;

        /// <summary>
        /// 日志记录
        /// </summary>
        protected ILogger logger;

        protected abstract SQLOptions SQLOptions { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connectionFactory">数据库连接</param>
        public DbContext(IDbConnectionFactory connectionFactory)
        {
            connectionFactory.SQLOptions = SQLOptions;
            this.connectionFactory = connectionFactory;
        }

        public DapperContext<T> Select<T>() where T : DbModel
        {
            var dapperContext = DapperContextFactory.Create<T>(connectionFactory);
            return dapperContext.Select();
        }

        public DapperContext<T> Select<T>(Expression<Func<T, object>> expression) where T : DbModel
        {
            var dapperContext = DapperContextFactory.Create<T>(connectionFactory);
            return dapperContext.Select(expression);
        }
    }
}